import { getStore, setStore } from '@/config/mUtils'
import { GET_USERINFO, SAVE_GEOHASH, RECORD_ADDRESS, ADD_CART, REDUCE_CART, INIT_BUYCART, RECORD_SHOPDETAIL, GET_DATA_ERROR, CHOOSE_ADDRESS, NEED_VALIDATION, SAVE_CART_ID_SIG, SAVE_ORDER_PARAM, ORDER_SUCCESS, SAVE_SHOPID, RECORD_USERINFO, CONFIRM_INVOICE, CLEAR_CART, CONFIRM_REMARK, CONFIRM_ADDRESS, CHOOSE_SEARCH_ADDRESS, SAVE_AVANDER, OUT_LOGIN, RETSET_NAME, ADD_ADDRESS, SAVE_ADDDETAIL } from '@/store/mutation-type'

export default {
    [GET_USERINFO](state, info) {
        if (state.userInfo && (state.userInfo.username !== info.username)) {
            return
        }
        if (!state.login) {
            return
        }
        if (!info.message) {
            state.userInfo = {...info }
        } else {
            state.userInfo = null
        }
    },
    [RECORD_SHOPDETAIL](state, detail) {
        state.shopDetail = detail
    },
    // 保存geohash
    [SAVE_GEOHASH](state, geohash) {
        state.geohash = geohash
    },
    // 记录当前的经度纬度
    [RECORD_ADDRESS](state, { latitude, longitude }) {
        state.latitude = latitude // 纬度
        state.longitude = longitude // 经度
    },
    // 加入购物车
    [ADD_CART](state, {
        shopid, // 商品id
        category_id, // 种类id
        item_id, // 
        food_id,
        name,
        price,
        specs,
        packing_fee,
        sku_id,
        stock
    }) {
        let cart = state.cartList; // 本地存储的购物车商品信息
        console.log(cart, '000');
        let shop = cart[shopid] = (cart[shopid] || {}); // 本次选择放入购物车的商品
        console.log(shop, '111');
        let category = shop[category_id] = (shop[category_id] || {}); // 筛选进入购物车商品的种类
        console.log(category, '222')
        let item = category[item_id] = (category[item_id] || {});
        console.log(item, '333')
        console.log(item[food_id])
        if (item[food_id]) { // 判断商品是否重复添加
            item[food_id]['num']++;
        } else {
            item[food_id] = {
                "num": 1,
                "id": food_id,
                "name": name,
                "price": price,
                "specs": specs,
                "packing_fee": packing_fee,
                "sku_id": sku_id,
                "stock": stock
            };
        }
        state.cartList = {...cart };
        // 存入localStorage
        setStore('buyCart', state.cartList);
    },
    // 移出购物车
    [REDUCE_CART](state, {
        shopid,
        category_id,
        item_id,
        food_id,
    }) {
        let cart = state.cartList
        let shop = (cart[shopid] || {})
        let category = (shop[category_id] || {})
        let item = (category[item_id] || {})
        if (item && item[food_id]) {
            if (item[food_id]['num'] > 0) {
                item[food_id]['num']--
                    state.cartList = {...cart }
                setStore('buyCart', state.cartList)
            } else {
                // 商品数量为0 则清空当前商品得信息
            }
        }
    },
    [INIT_BUYCART](state) {
        let initCart = getStore('buyCart')
        if (initCart) {
            state.cartList = JSON.parse(initCart)
        }
    },
    [GET_DATA_ERROR](state, detail) {
        state.shopDetail = detail
    },
    // 选择的地址
    [CHOOSE_ADDRESS](state, { address, index }) {
        state.choosedAddress = address
        state.addressIndex = index
    },
    // 保存下单需要验证的返回值
    [NEED_VALIDATION](state, needValidation) {
        state.needValidation = needValidation
    },
    // 保存下单后购物id和sig
    [SAVE_CART_ID_SIG](state, {
        cart_id,
        sig
    }) {
        state.cart_id = cart_id
        state.sig = sig
    },
    // 保存下单参数，用户验证页面调用
    [SAVE_ORDER_PARAM](state, orderParam) {
        state.orderParam = orderParam
    },
    // 下单成功，保存订单返回信息
    [ORDER_SUCCESS](state, order) {
        state.cartPrice = null
        state.orderMessage = order
    },
    // 保存商铺id
    [SAVE_SHOPID](state, shopid) {
        state.shopid = shopid
    },
    [RECORD_USERINFO](state, info) {
        state.userInfo = info
        state.login = true
        setStore('user_id', info.user_id)
    },
    [CONFIRM_INVOICE](state, invoice) {
        state.invoice = invoice
    },
    [CLEAR_CART](state, shopid) {
        state.cartList[shopid] = null
        state.cartList = {...state.cartList }
        setStore('buyCart', state.cartList)
    },
    [CONFIRM_REMARK](state, {
        remarkText,
        inputText
    }) {
        state.remarkText = remarkText
        state.inputText = inputText
    },
    // 确认订单页添加新得地址
    [CONFIRM_ADDRESS](state, newAddress) {
        state.newAddress.push(newAddress)
    },
    [CHOOSE_SEARCH_ADDRESS](state, place) {
        state.searchAddress = place
    },
    // 保存图片
    [SAVE_AVANDER](state, imgPath) {
        state.imgPath = imgPath
    },
    // 退出登录
    [OUT_LOGIN](state) {
        state.userInfo = {}
        state.login = false
    },
    // 修改用户名
    [RETSET_NAME](state, username) {
        state.userInfo = Object.assign({}, state.userInfo, { username })
    },
    // 新增地址
    [ADD_ADDRESS](state, obj) {
        state.removeAddress = [obj, ...state.removeAddress]
    },
    // 添加地址name
    [SAVE_ADDDETAIL](state, addAddress) {
        state.addAddress = addAddress
    }
}
